import { applyMiddleware, legacy_createStore as createStore } from 'redux'
import { thunk } from 'redux-thunk'
/**
 * 根据配置创建一个仓库
 */
function configureStore(options = {}) {
  const {
    reducer, // reducer 处理器函数
    middleware, // 中间件组成的数组
    preloadedState, // 初始状态
  } = options

  // 默认的中间件组成的数组
  const defaultMiddlewares = () => [thunk]

  // 获取中间件组成的数组
  const middlewares = typeof middleware === 'function' ?
    middleware(defaultMiddlewares) :
    defaultMiddlewares()

  // 配置中间件
  // 创建仓库的 store
  // reducer 处理函数和仓库状态
  const store = applyMiddleware(...middlewares)(createStore)(reducer, preloadedState)

  return store
}

export default configureStore
